import axios from 'axios'

export const getCity = () => {
  const localCity = localStorage.getItem('local_city')

  if (localCity) {
    return Promise.resolve(JSON.parse(localCity))
  }

  return new Promise((resolve, reject) => {
    const myCity = new window.BMapGL.LocalCity()
    myCity.get(async result => {
      const cityName = result.name

      const { data } = await axios.get(`http://127.0.0.1:8080/area/info?name=${cityName}`)

      if(data.status !== 200) {
        reject(data.description)
      }

      resolve(data.body)
      localStorage.setItem('local_city', JSON.stringify(data.body))
    })
  })

}